package com.cn.ccp.listener;

import javax.faces.application.NavigationHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.cn.ccp.service.system.MenuService;
import com.cn.ccp.util.CCPContants;
@SuppressWarnings("serial")
public class PermissionListener implements PhaseListener{
	private Logger log = Logger.getLogger(PermissionListener.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void afterPhase(PhaseEvent event) {
		FacesContext context = event.getFacesContext();
		UIViewRoot viewRoot = context.getViewRoot();
		log.info("----"+viewRoot.getViewId());
		FacesContext fc = event.getFacesContext();
        // Check to see if they are on the login page.
        boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true: false;
        log.info("----loginPage = "+loginPage);
        if(fc.getViewRoot().getViewId().lastIndexOf("regist") > -1)
        	return;
        
        //判断是否为登录页面请求
        
        if (!loginPage && !isLogin(context)) {
             NavigationHandler nh = fc.getApplication().getNavigationHandler();
             nh.handleNavigation(fc, null, "logout");
        }else if(loginPage){
        	boolean hasRegist = false;
    		String _serialCode = RegisterPropertiesUtils.getInstance().getSerialCode();
    		hasRegist = SerialCodeUtils.validateSerialCode(_serialCode);
    		log.info("---------------------------_serialCode = "+_serialCode);
    		log.info("---------------------------hasRegist = "+hasRegist);
        	if(!hasRegist){
        		 NavigationHandler nh = fc.getApplication().getNavigationHandler();
	             nh.handleNavigation(fc, null, "regist");
        	}else if(isLogin(context)){
	        	 NavigationHandler nh = fc.getApplication().getNavigationHandler();
	             nh.handleNavigation(fc, null, "loginSucess");
        	}
        }
	}

	@Override
	public void beforePhase(PhaseEvent phaseEvent) {
		FacesContext facesContext = phaseEvent.getFacesContext();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        response.addHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "must-revalidate");
	}

	@Override
	public PhaseId getPhaseId() {
		return PhaseId.RESTORE_VIEW;
	}

	private boolean isLogin(FacesContext context) {
		boolean login = false;
		Object user = context.getExternalContext().getSessionMap()
				.get("userBean");
		if (user != null) {
			login = true;
		}
		log.info("-------------login = "+login);
		return login;
	}
	public MenuService getMenuService() {
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		MenuService menuService = (MenuService) fcontextutils.getBean("menuService");
		return menuService;
	}
}
